#include "BattlePage.h"

BattlePage::~BattlePage() {
    delete ui;
}

void BattlePage::SetGameMode(int Mode) {
    this->gameMode = Mode;
}

void BattlePage::initialize() {
    if (gameMode == 0) {

    }
    else if (gameMode == 1) {

    }
    else if (gameMode == 2) {

    }
    else if (gameMode == 3) {

    }
    else {

    }
}

void BattlePage::paintEvent(int *event) {
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);

    painter.setPen(Qt::black);
    for (int i = 1; i <= 19; i++) {
        painter.drawLine(chessboard[1][i],chessboard[19][i]);
        painter.drawLine(chessboard[i][1],chessboard[i][19]);
    }

    if (checkerBoard.Route.RecentStep().PLAYERID() == checkerBoard.Route.showFirstPlayerID()) {
        painter.setBrush(Qt::black);
    }
    else {
        painter.setBrush(Qt::white);
    }

    if (cursorRow != -1 && cursorCol != -1) {
        QRect rec(chessboard[cursorCol][cursorRow].x()-8/2, chessboard[cursorCol][cursorRow].y()-8/2, 8, 8);
        painter.drawRect(rec);
    }

    for (int i = 1; i <= 19; i++) {
        for (int j = 1; j <= 19; j++) {
            if (checkerBoard.Board[i][j].PLAYERID() != -1) {
                if (checkerBoard.Board[i][j] == checkerBoard.Route.showFirstPlayerID()) {
                    painter.setBrush(Qt::black);
                }
                else {
                    painter.setBrush(Qt::white);
                }
                painter.drawEllipse(chessboard[j][i].x()-20/2, chessboard[j][i].y()-20/2, 20, 20);
            }
        }
    }
}

void BattlePage::mouseMoveEvent(int *event) {
    // todo fix 455 & 5
    if (event->x() >= 5 && event-> x <= 455 && event->y >= 5 && event->y <= 455) {
        setCursor(Qt::BlankCursor);
        for (int i = 1; i <= 19; i++) {
            for (int j = 0; j <= 19; j++) {
                float x = event->x(), y = event->y();
                if (x>=(chessboard[i][j].x()-15) && x<(chessboard[i][j].x()+15) && y>=(chessboard[i][j].y()-15) && y<(chessboard[i][j].y()+15)) {
                    cursorRow = j;
                    cursorRow = i;
                    if (checkerBoard.Board[cursorRow][cursorCol].PLAYERID() != -1) {
                        setCursor(Qt::ForbiddenCursor);
                    }
                }
            }
        }
    }
    else {
        setCursor(Qt::ArrowCursor);
    }
    update();
}

void BattlePage::mouseReleaseEvent(int *event) {
    if (gameMode == network || gameMode == network_none) {
        int s_server = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);

        sockaddr_in server_addr; memset(&server_addr, 0, sizeof(sockaddr_in));
        server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); server_addr.sin_port = htons(8086);

        bind(s_server, (sockaddr*)&server_addr,sizeof(sockaddr));
        listen(s_server,20);

        sockaddr_in client_addr;
        socklen_t client_addr_size = sizeof(client_addr);
        int c_server = accept(s_server,(sockaddr*)&client_addr,&client_addr_size);
        if (checkerBoard.Board[cursorRow][cursorCol] != -1) {
            checkerBoard.PlaceNode(cursorRow, cursorCol, yourid);
            string str = "";
            str += cursorRow;
            str += ".";
            str += cursorCol;
            str += ".";
            str += yourid;
            char *sendmsg = StringToCharArray(str);
            write(c_server, sendmsg, sizeof(sendmsg));
            close(c_server);
            close(s_server);
        }
    }
}

char * BattlePage::StringToCharArray(string str) {
    char arr = new char[str.length()];
    for (int i = 0; i < str.length(); i++) {
        arr[i] = str.at(i);
    }
    return arr;
}

int * BattlePage::NetWorkTranslation(char *arr) {
    int *arr = new int[3];
    string str[3];
    for (int i = 0, j = 0; j < 3 && i < 256; i++) {
        if(message[i] != '.' || message[i] != '-') {
            str[j] += message[i];
        }
        if (message[i] == '.' || message[i] == '-') {
            j++;
        }
    }
    for (int i = 0; i < 3; i++) {
        arr[i] = atoi(str[i].c_str());
    }
    return arr;
}

int GameEvent::ServerMode() {}

